package com.biz.primus.management.cat.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.biz.primus.common.vo.SpringPageVO;
import com.biz.primus.management.base.controller.AbstractBaseController;
import com.biz.primus.management.cat.feign.CatFeignClient;
import com.biz.primus.model.cat.enums.CatSaleStatusEnum;
import com.biz.primus.model.cat.enums.CatStatus;
import com.biz.primus.model.cat.vo.CatQueryVo;
import com.biz.primus.model.cat.vo.CatVo;

/**
 * @author yanweijin
 * @date 2017/9/13
 */
@Controller
@RequestMapping("demo/cats")
public class CatController extends AbstractBaseController {

    private final CatFeignClient catFeignClient;

    @Autowired
    public CatController(CatFeignClient catFeignClient) {
        this.catFeignClient = catFeignClient;
    }

    @GetMapping
    @PreAuthorize("hasAuthority('OPT_CAT_LIST')")
    public ModelAndView list(@ModelAttribute("reqVo") CatQueryVo reqVo) {

        SpringPageVO<CatVo> catPage = catFeignClient.findAll(reqVo);
        return new ModelAndView("demo/cats/searchResult", "catPage", catPage);
    }


    @GetMapping("listByStatus")
    @PreAuthorize("hasAuthority('OPT_CAT_LIST')")
    public ModelAndView list(@RequestParam(value = "status", required = false, defaultValue = "ALIVE") CatStatus status) {

        List<CatVo> cats = catFeignClient.listByStatus(status);
        return new ModelAndView("demo/cats/list", "cats", cats).addObject("enabled", status == CatStatus.ALIVE);
    }

    @GetMapping("listBySaleStatus")
    @PreAuthorize("hasAuthority('OPT_CAT_LIST')")
    public ModelAndView listBySaleStatus(@RequestParam(value = "saleStatus", required = false) CatSaleStatusEnum saleStatus) {

        List<CatVo> cats = catFeignClient.listBySaleStatus(saleStatus);
        return new ModelAndView("demo/cats/list", "cats", cats);
    }

    @RequestMapping("{id}")
    @PreAuthorize("hasAuthority('OPT_CAT_LIST')")
    public ModelAndView edit(@PathVariable Long id) {

        return new ModelAndView("demo/cats/new", "cat", catFeignClient.get(id));
    }

    @GetMapping("new")
    @PreAuthorize("hasAuthority('OPT_CAT_EDIT')")
    public ModelAndView add() {

        return new ModelAndView("demo/cats/new");
    }

    @PostMapping("save")
    @PreAuthorize("hasAuthority('OPT_CAT_EDIT')")
    public ModelAndView save(@Valid CatVo catReqVO, BindingResult result) {

        error(result);
        catFeignClient.save(catReqVO);
        return new ModelAndView("redirect:/demo/cats.do");
    }

}
